package com.zsc.wxm.ysh.anjo.house.controller;


import com.alibaba.fastjson.JSON;
import com.zsc.wxm.ysh.anjo.house.service.ResourcesService;
import com.zsc.wxm.ysh.anjo.result.PageResult;
import com.zsc.wxm.ysh.anjo.result.Result;
import com.zsc.wxm.ysh.anjo.service.base.enums.resources.HouseStatus;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.dto.EstateDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.dto.MyRentHouseDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.dto.ResourcesDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.qo.ResourcesQuery;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author playmaker
 * @since 2021-01-04
 */
@RestController
@RequestMapping("/house/resources")
@Slf4j
public class ResourcesController {

    @Resource
    private ResourcesService service;

    @PostMapping("/batch-upload-es")
    @ApiOperation("将数据上传到es中")
    public Result batchAddHouseData() {
        try {
            service.batchAddHouseData();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.ok("上传成功");
    }

    @PostMapping("/add")
    @ApiOperation("新增房源")
    public Result add(@Validated ResourcesDTO params, @RequestParam MultipartFile[] files) {
        log.info("add 传入的参数: {}，图片大小：{}", JSON.toJSONString(params), files.length);
        Map<String,Object> data = new HashMap<>(1);
        data.put("data", service.addResources(params,files));
        return Result.ok("增加成功", data);
    }

    @PostMapping("/update")
    @ApiOperation("更新房源")
    public Result update(@Validated ResourcesDTO params, @RequestParam(required = false) MultipartFile[] files) {
        service.updateItem(params, files);
        return Result.ok("更新成功");
    }

    @PostMapping("/list-page")
    @ApiOperation("分页查询房源数据")
    public PageResult listPage(@RequestBody ResourcesQuery params) {
        return service.listPage(params);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除房源")
    public Result delete(@PathVariable("id") Long id) {
        return service.deleteById(id);
    }


    @GetMapping("/get-info-by-id/{id}")
    @ApiOperation("根据id查询详情")
    public Result getInfoById(@PathVariable("id") Long id) {
        Map<String, Object> data = new HashMap<>();
        data.put("info", service.getInfoById(id));
        return Result.ok("获取成功", data);
    }

    @PutMapping("/update-status/{id}/{status}")
    @ApiOperation("更新房源状态")
    public Result updateStatus(@PathVariable("id") Long id, @PathVariable("status") int status) {
        if (HouseStatus.PASSED.getValue() == status) {
            status = HouseStatus.AVAILABLE.getValue();
        }
        service.updateStatus(id, status);
        return Result.ok("更新成功！");
    }

    @PostMapping("/get-user-house")
    @ApiOperation("获取房东房源")
    public Result getUserHouseByNameAndPhone(@RequestParam("userId") Long userId) {
        Map<String, Object> data = new HashMap<>();
        data.put("data", service.getUserHouseByNameAndPhone(userId));
        return Result.ok("获取成功", data);
    }

    @GetMapping("/get-es-data-vo-by-id/{id}")
    @ApiOperation("获取ES视图对象")
    public Result getEsDataVoById(@PathVariable("id") Long id) {
        Map<String, Object> data = new HashMap<>(1);
        data.put("esDataVo", service.getEsDataVoById(id));
        return Result.ok("获取成功", data);
    }


    @PostMapping("/get-my-rent-house")
    @ApiOperation("获取我出租的房源")
    public PageResult getMyRentHouse(@Validated @RequestBody MyRentHouseDTO param) {
        return service.getMyRentHouse(param);
    }

    @PostMapping("/entrust-house")
    @ApiOperation("房源委托")
    public Result entrustHouse(@Validated EstateDTO estateDto, @Validated ResourcesDTO resourcesDto,
                               @RequestParam MultipartFile[] files) {
        service.entrustHouse(estateDto, resourcesDto, files);
        return Result.ok("委托成功");
    }




}

